package cn.endeavour.order.controller;


import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.basic.util.PageList;
import cn.endeavour.order.domain.ProductOrder;
import cn.endeavour.order.query.ProductOrderQuery;
import cn.endeavour.order.service.IProductOrderService;
import cn.endeavour.order.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 产品订单接口
 */
@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Autowired
    private IProductOrderService productOrderService;


    /**
     * 管理员：管理员查询订单接口 - 平台管理员所有店铺的所有订单，店铺管理员查询当前店铺的所有订单
     * @param productOrderQuery 订单查询参数
     * @param request
     * @return
     */
    @PostMapping("/admin")
    public PageList<ProductOrder> adminProductOrder(@RequestBody ProductOrderQuery productOrderQuery, HttpServletRequest request){
        return productOrderService.queryAdminOrder(productOrderQuery, request);
    }


    /**
     * 用户：查询订单接口 - 只能查询自己的订单
     * @param productOrderQuery 订单查询参数
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<ProductOrder> userProductOrder(@RequestBody ProductOrderQuery productOrderQuery, HttpServletRequest request){
        return productOrderService.queryUserOrder(productOrderQuery, request);
    }
    
    /**
     * 提交
     * @param params 参数
     * @param request 请求
     * @return 处理结果
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){

        try{
            String payData = productOrderService.productOrderSubmit(params, request);
            return AjaxResult.me().setResultObj(payData);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("下单失败！"+e.getMessage());
        }

    }
}
